package com.cxrj.zhsq.userManage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.userManage.dao.DirectoriesMapper;
import com.cxrj.zhsq.userManage.dao.OrganizationMapper;
import com.cxrj.zhsq.userManage.entity.Directories;
import com.cxrj.zhsq.userManage.service.DirectoriesService;

@Service
public class DirectoriesServiceImpl implements DirectoriesService {
	
	@Resource
	private DirectoriesMapper directoriesMapper;
	@Resource
	private OrganizationMapper organizationMapper;

	@Override
	public Map<String, Object> selectDirectoriesPager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		String organizationId = request.getParameter("organizationId");
		List<Directories> list = new ArrayList<Directories>();
		
		// 总记录数
		String recordsTotal = "0";
		if(organizationId != ""){
			// 获取数据库总记录数
			sqlMap.put("start", Integer.parseInt(start));
			sqlMap.put("condition", " where 1=1 ");
			sqlMap.put("condition", "  where  organization_id = '" + organizationId + "'" );
			recordsTotal = directoriesMapper.getDirectoriesCount("  where  organization_id = '" + organizationId + "'") + "";
			
			sqlMap.put("length", Integer.parseInt(length));
			list = directoriesMapper.selectDirectoriesPager(sqlMap);
			for (int i = 0; i < list.size(); i++) {
				list.get(i).setOrganizationName(organizationMapper.selectByPrimaryKey(Integer.parseInt(list.get(i).getOrganizationId())).getOrganizationName());
			}
		}

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public void insertDirectories(String organizationId,String name,String tel,String id) {
		Directories directories = new Directories();
		directories.setOrganizationId(organizationId);
		directories.setName(name);
		directories.setTel(tel);
		directories.setCreateTime(new Date());
		if(id == ""){
			directories.setId(UUID.randomUUID().toString());
			directoriesMapper.insertSelective(directories);
		}else{
			directories.setId(id);
			directoriesMapper.updateByPrimaryKeySelective(directories);
		}
	}

	@Override
	public Map<String, Object> selectDirectoriesById(String id) {
		Map<String, Object> resultMap = new HashMap<String,Object>();
		Directories directories = directoriesMapper.selectByPrimaryKey(id);
		resultMap.put("directories", directories);
		return resultMap;
	}

	@Override
	public void deleteDirectories(String id) {
		directoriesMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Map<String, Object> selectDirectories(String id) {
		Map<String, Object> resultMap = new HashMap<String,Object>();
		resultMap.put("list", directoriesMapper.selectDirectories(id));
		return resultMap;
	}

}
